<?php
namespace app\controller\admin;

use app\BaseController;
use app\model\ExamQuestion;
use app\model\ExamImage;
use think\facade\View;

class Question extends BaseController
{
    // 添加题目
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            
            // 处理选项
            if (in_array($data['type'], [2, 3])) {
                $options = [];
                foreach ($data['options'] as $key => $value) {
                    if (!empty($value)) {
                        $options[$key] = $value;
                    }
                }
                $data['options'] = json_encode($options, JSON_UNESCAPED_UNICODE);
            } else {
                $data['options'] = null;
            }
            
            // 处理图片内容
            $data['content'] = $this->processImages($data['content'], null, 1);
            $data['explanation'] = $this->processImages($data['explanation'], null, 3);
            
            $question = new ExamQuestion();
            $question->save($data);
            
            // 保存选项图片
            if (in_array($data['type'], [2, 3]) && !empty($data['option_images'])) {
                foreach ($data['option_images'] as $key => $image) {
                    if (!empty($image)) {
                        $img = new ExamImage();
                        $img->question_id = $question->id;
                        $img->url = $image;
                        $img->type = 2; // 选项图片
                        $img->save();
                    }
                }
            }
            
            return json(['code' => 0, 'msg' => '添加成功']);
        }
        
        return View::fetch();
    }
    
    // 处理内容中的图片
    private function processImages($content, $questionId, $type)
    {
        // 提取临时图片URL
        preg_match_all('/<img[^>]+src="([^"]+)"/i', $content, $matches);
        $tempImages = $matches[1] ?? [];
        
        foreach ($tempImages as $tempUrl) {
            if (strpos($tempUrl, '/uploads/exam/temp/') !== false) {
                // 移动图片到正式目录
                $newPath = str_replace('/temp/', '/', $tempUrl);
                $tempFile = public_path() . $tempUrl;
                $newFile = public_path() . $newPath;
                
                if (file_exists($tempFile)) {
                    // 创建目录
                    $dir = dirname($newFile);
                    if (!is_dir($dir)) {
                        mkdir($dir, 0755, true);
                    }
                    
                    rename($tempFile, $newFile);
                    $content = str_replace($tempUrl, $newPath, $content);
                    
                    // 保存图片记录
                    $img = new ExamImage();
                    $img->question_id = $questionId;
                    $img->url = $newPath;
                    $img->type = $type;
                    $img->save();
                }
            }
        }
        
        return $content;
    }
}